<template>
  <el-dialog v-model="dialogVisible" width="40%" :title="`${title}`" @close="cancel">
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :model="formData"
      label-width="auto"
      style="overflow-x: hidden; padding: 0px 20px; box-sizing: border-box"
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item class="form-itme-width" label="检验类别" prop="category">
            <el-select v-model="formData.category" placeholder="请输入检验类别" clearable>
              <el-option
                v-for="(item, index) in DC_INSPECTION_CLASS"
                :key="index"
                :value="item.label"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="检验物料" prop="materialNumberCollection">
            <dc-select-dialog
              v-model="formData.materialNumberCollection"
              placeholder="请选择检验物料"
              objectName="SnCheckMaterial"
              type="input"
              :params="{
                fuseorgid: 100006,
                currentPage: null,
                pageSizeName: null,
              }"
              @change="changeMaterial"
              :multiple="true"
              :multiple-limit="100"
              :clearable="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="参考依据" prop="referenceBasisId">
            <dc-upload
              v-model="formData.referenceBasisId"
              :limit="1"
              :targetType="Const.targetType.CUSTOMER"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
        <el-button @click="cancel(ruleFormRef)">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import dayjs from 'dayjs';
import Const from '@/const';
import Api from '@/api/index';

const { proxy } = getCurrentInstance();

// 数据字典
const { DC_INSPECTION_CLASS } = proxy.useCache([{ key: 'DC_INSPECTION_CLASS' }]);

const data = reactive({
  loading: true,
  rules: {
    category: [{ required: true, message: '检验类别为必选项', trigger: 'change' }],
    materialNameCollection: [{ required: true, message: '检验物料为必选项', trigger: 'change' }],
  },
  dialogVisible: false,
  formData: {},
  ids: [],
  title: '新增',
});
const { rules, dialogVisible, formData, title } = toRefs(data);
const ruleFormRef = ref(null);

const emit = defineEmits(['success']);

// 检验物料
const changeMaterial = e => {
  console.log(e);
  if (e.length > 0) {
    // formData.value.materialNumberCollection = e.map(item => item.fnumber);
    formData.value.materialNameCollection = e.map(item => item.fname).join(',');
  } else {
    // formData.value.materialNumberCollection = [e.fnumber];
    formData.value.materialNameCollection = e.fname;
  }
};
// 打开添加弹窗
const openDialog = async row => {
  dialogVisible.value = true;
  ruleFormRef.value.resetFields();
  title.value = row ? '编辑' : '新增';
  data.formData = row || {};
  if (Array.isArray(row.referenceBasisId) && row.referenceBasisId.length > 0) {
    let referenceBasisIdsList = row.referenceBasisId.map(item => {
      return item.id;
    });
    data.formData.referenceBasisId = referenceBasisIdsList.join(',');
  }
};

// 弹出框提交表单
const submitForm = async formEl => {
  formEl.validate(async valid => {
    if (valid) {
      let newrow = Object.assign(formData.value, {});
      newrow.configDate = dayjs().valueOf();
      if (Array.isArray(newrow.referenceBasisId) && newrow.referenceBasisId.length > 0) {
        let referenceBasisIdsList = newrow.referenceBasisId.map(item => {
          return item.id;
        });
        newrow.referenceBasisId = referenceBasisIdsList.join(',');
      }

      if (
        Array.isArray(newrow.materialNumberCollection) &&
        newrow.materialNumberCollection.length > 0
      ) {
        let list = newrow.materialNumberCollection.map(item => {
          return item.id;
        });
        newrow.materialNumberCollection = list.join(',');
      }
      console.log(newrow);

      const res = await Api.qms.sn.sipOrderSubmit(newrow);
      const { code, msg } = res.data;
      if (code === 200) {
        cancel();
        emit('success');
        proxy.$message.success(msg);
      }
    }
  });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  formData.value = {};
};

defineExpose({
  openDialog,
});
</script>

<style scoped lang="scss"></style>
